package com.fulihui.systemcore.biz.conv;

import com.fulihui.systemcore.dal.dataobj.PhoneRegLoc;
import com.fulihui.systemcore.dto.PhoneRegLocDTO;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Willard.Hu on 2016/11/29 0029.
 */
public class PhoneRegLocConv {

    public static PhoneRegLoc toDO(PhoneRegLocDTO dto) {
        if (dto == null) {
            return null;
        }
        PhoneRegLoc db = new PhoneRegLoc();
        db.setPrefix(dto.getPrefix());
        db.setProvince(dto.getProvince());
        db.setProvincename(dto.getProvincename());
        db.setCity(dto.getCity());
        db.setCityname(dto.getCityname());
        db.setSupplier(dto.getSupplier());
        db.setSuit(dto.getSuit());
        db.setGmtCreate(dto.gmtCreate());
        db.setGmtModified(dto.gmtModified());
        return db;
    }

    public static PhoneRegLocDTO toDTO(PhoneRegLoc db) {
        if (db == null) {
            return null;
        }
        PhoneRegLocDTO dto = new PhoneRegLocDTO();
        dto.setPrefix(db.getPrefix());
        dto.setProvince(db.getProvince());
        dto.setProvincename(db.getProvincename());
        dto.setCity(db.getCity());
        dto.setCityname(db.getCityname());
        dto.setSupplier(db.getSupplier());
        dto.setSuit(db.getSuit());
        dto.gmtCreate(db.getGmtCreate());
        dto.gmtModified(db.getGmtModified());
        return dto;
    }

    public static List<PhoneRegLocDTO> toList(List<PhoneRegLoc> list) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return list.stream().map(PhoneRegLocConv::toDTO).collect(Collectors.toList());
    }

    /**
     * 转换成 Map 对象，key是手机号前七位，map是归属地信息
     *
     * @return {@link PhoneRegLocDTO} 映射
     */
    public static Map<Integer, PhoneRegLocDTO> toMap(List<PhoneRegLoc> list) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyMap();
        }
        Map<Integer, PhoneRegLocDTO> map = new HashMap<>();
        list.forEach(it -> map.put(it.getPrefix(), toDTO(it)));
        return map;
    }
}
